package code.class1;

import cn.hutool.core.util.ArrayUtil;

/**
 * https://leetcode.cn/problems/reverse-string/description/
 * 反转字符串
 */
public class Code05_reverseStr1 {
    public static void main(String[] args) {
        String str = "hello";
        char[] chars = str.toCharArray();
        reverseString(chars);
        System.out.println(ArrayUtil.toString(chars));
    }

    public static void reverseString(char[] arr) {
        int p1 = 0, p2 = arr.length - 1;
        while (p1 < p2) {
            swap(arr, p1++, p2--);
        }
    }

    public static void swap(char[] arr, int x, int y) {
        char t = arr[x];
        arr[x] = arr[y];
        arr[y] = t;
    }
}
